package me.kuye.jkuye.core;

import java.util.Enumeration;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import me.kuye.jkuye.render.Render;
import me.kuye.jkuye.render.RenderFactory;

public abstract class Controller {
	private HttpServletRequest request;
	private HttpServletResponse response;
	private String urlPara;
	private String[] urlParaArray;

	private static final String[] NULL_URL_PARA_ARRAY = new String[0];
	private static final String URL_PARA_SEPARATOR = Config.getConstants().getUrlParaSeparator();

	public void init(HttpServletRequest request, HttpServletResponse response, String urlPara) {
		this.request = request;
		this.response = response;
		this.urlPara = urlPara;
	}

	public HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	public HttpServletResponse getResponse() {
		return response;
	}

	public void setResponse(HttpServletResponse response) {
		this.response = response;
	}

	public String getUrlPara() {
		return urlPara;
	}

	public void setUrlPara(String urlPara) {
		this.urlPara = urlPara;
		this.urlParaArray = null;
	}

	public Controller setAttr(String name, Object value) {
		request.setAttribute(name, value);
		return this;
	}

	public Controller removeAttr(String name) {
		request.removeAttribute(name);
		return this;
	}

	public Controller setAttrs(Map<String, Object> attrMap) {
		for (Map.Entry<String, Object> entry : attrMap.entrySet())
			request.setAttribute(entry.getKey(), entry.getValue());
		return this;
	}

	public String getPara(String name) {
		return request.getParameter(name);
	}

	public String getPara(String name, String defaultValue) {
		String result = request.getParameter(name);
		return result != null && !"".equals(result) ? result : defaultValue;
	}

	public Map<String, String[]> getParaMap() {
		return request.getParameterMap();
	}

	public Enumeration<String> getParaNames() {
		return request.getParameterNames();
	}

	public String[] getParaValues(String name) {
		return request.getParameterValues(name);
	}

	public Integer[] getParaValuesToInt(String name) {
		String[] values = request.getParameterValues(name);
		if (values == null)
			return null;
		Integer[] result = new Integer[values.length];
		for (int i = 0; i < result.length; i++)
			result[i] = Integer.parseInt(values[i]);
		return result;
	}

	public Long[] getParaValuesToLong(String name) {
		String[] values = request.getParameterValues(name);
		if (values == null)
			return null;
		Long[] result = new Long[values.length];
		for (int i = 0; i < result.length; i++)
			result[i] = Long.parseLong(values[i]);
		return result;
	}

	public String getPara() {
		if ("".equals(urlPara))
			urlPara = null;
		return urlPara;
	}

	public String getPara(int index) {
		if (index < 0)
			return getPara();

		if (urlParaArray == null) {
			if (urlPara == null || "".equals(urlPara))
				urlParaArray = NULL_URL_PARA_ARRAY;
			else
				urlParaArray = urlPara.split(URL_PARA_SEPARATOR);

			for (int i = 0; i < urlParaArray.length; i++)
				if ("".equals(urlParaArray[i]))
					urlParaArray[i] = null;
		}
		return urlParaArray.length > index ? urlParaArray[index] : null;
	}
	/*
	 * the Render
	 * 
	 */

	private static final RenderFactory renderFactory = RenderFactory.getInstance();

	private Render render;

	public Render getRender() {
		return render;
	}

	public void render(Render render) {
		this.render = render;
	}

	public void render(String view) {
		render = renderFactory.getRender(view);
	}
}
